package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.activity.NewResiginActivity;
import com.mission.schedule.activity.PhoneCodeLoginActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.List;
import java.util.Map;

/**
 * 清单类别对话框
 * Created by liwei on 2017/4/18.
 */

public class NoteTypeMenuDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    TextView last,next,name;



    public NoteTypeMenuDialog(@NonNull final Context context, @StyleRes int themeResId, final WindowManager windowManager,
                              final List<NoteTypeBean.ListBean> bean,final int position) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_note_type_menu, null);
        setContentView(view);

        last = (TextView) view.findViewById(R.id.last);
        next = (TextView) view.findViewById(R.id.next);
        name = (TextView) view.findViewById(R.id.name);

//        if (bean.get(position - 1).orderId < 0){
//            last.setClickable(false);
//            last.setEnabled(false);
//            last.setTextColor(context.getResources().getColor(R.color.gongkai_txt));
//        }
//        if (bean.get(position).orderId < 0){
//            next.setClickable(false);
//            next.setEnabled(false);
//            next.setTextColor(context.getResources().getColor(R.color.gongkai_txt));
//        }
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getDBcApplication().updateNoteTyeOrderId(bean.get(position).localId,bean.get(position + 1).localId,
                        bean.get(position + 1).orderId,bean.get(position).orderId);
                callBack.updateTypeOrder();
                dismiss();
            }
        });
        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.getDBcApplication().updateNoteTyeOrderId(bean.get(position).localId,bean.get(position - 1).localId,
                        bean.get(position - 1).orderId,bean.get(position).orderId);
                callBack.updateTypeOrder();
                dismiss();
            }
        });
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                NoteTypeNameEditDialog noteTypeNameEditDialog = new NoteTypeNameEditDialog(context,R.style.dialog_translucent,
                        windowManager,bean.get(position).localId+"",bean.get(position).content,1);
                noteTypeNameEditDialog.show();
                noteTypeNameEditDialog.setCallBack(new NoteTypeNameEditDialog.CallBack() {
                    @Override
                    public void updateTypeName(String name) {
                        callBack.updateTypeName(name);
                    }
                });
            }
        });
        view.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.add(bean.get(position).styleId+"");
                dismiss();
            }
        });
        view.findViewById(R.id.copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.copy();
                dismiss();
            }
        });
        view.findViewById(R.id.set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    callBack.set();
                } else {
                    VipDailog();
                }
                dismiss();
            }
        });
        view.findViewById(R.id.sort).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sort(bean.get(position).localId);
                dismiss();
            }
        });
    }
    private void VipDailog() {
        final android.app.AlertDialog builder = new android.app.AlertDialog.Builder(context).create();

        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alter);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        delete_ok.setText("注册");
        delete_ok.setTextColor(Color.RED);
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent newt = new Intent(context,PhoneCodeLoginActivity.class);
                newt.putExtra("type", 1);
                newt.putExtra("login",true);
                context.startActivity(newt);
            }
        });
        TextView cancle_tv = (TextView) window.findViewById(R.id.cancle_tv);
        cancle_tv.setText("忽略");
        cancle_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);


        delete_tv.setText("该功能是正式用户功能\n请注册正式用户");

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth() - 30;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void updateTypeOrder();
        void updateTypeName(String name);
        void add(String styleid);
        void copy();
        void set();
        void sort(int localid);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
